package threading;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-08-20
 * Time:14:58
 */
public class Demo7 {
    // 100 亿
    public static final long COUNT = 100_0000_0000L;

    public static void main(String[] args) {
        serial();
        //concurrency();
    }

    // 串行执行任务
    public static void serial() {
        // 记录 ms 级别的时间戳.
        long beg = System.currentTimeMillis();

        long a = 0;
        for (long i = 0; i < COUNT; i++) {
            a++;
        }

        a = 0;
        for (long i = 0; i < COUNT; i++) {
            a++;
        }

        long end = System.currentTimeMillis();
        System.out.println("执行的时间间隔: " + (end - beg) + " ms");
    }

    // 并发执行任务
    public static void concurrency() {
        long beg = System.currentTimeMillis();

        Thread t1 = new Thread(() -> {
            long a = 0;
            for (long i = 0; i < COUNT; i++) {
                a++;
            }
        });
        Thread t2 = new Thread(() -> {
            long a = 0;
            for (long i = 0; i < COUNT; i++) {
                a++;
            }
        });

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("执行的时间间隔: " + (end - beg) + " ms");
    }
}
